package com.koron.css2.projectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.baseConfig.queryBean.MeterModelQueryBean;
import com.koron.css2.baseConfig.vo.MeterModelVO;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.BusinessAreaBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.queryBean.BusinessAreaQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

public class PjMeterList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
            MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
            Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");
            MeterModelMapper mapper = factory.getMapper(MeterModelMapper.class);
            PubAdminAreaMapper pubAdminAreaMapper = factory.getMapper(PubAdminAreaMapper.class);

            // 1. 列表查询
            List<PjMeter> pjMeterList = pjMeterMapper.getList(pjMeterQuery);
            if (pjMeterList != null && pjMeterList.size() > 0) {
                for (PjMeter pjMeter : pjMeterList) {
                    // 2. 临时客户信息
                    CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoById(pjMeter.getUserCtmTempId(), CtmInfoBean.CTMTEMP);
                    // 20220519: 兼容单个客户的情况
                    CtmInfoBean ctmInfoBeanCopy = ctmInfoBean.clone();
                    // 2.1 clientAddr->clientAddrId;clientAddr转中文
                    if (StringUtils.isNotEmpty(ctmInfoBeanCopy.getClientAddr())) {
                        ctmInfoBeanCopy.setClientAddrId(ctmInfoBeanCopy.getClientAddr());
                        String area = pubAdminAreaMapper.selectFullNameById(ctmInfoBeanCopy.getClientAddr());
                        ctmInfoBeanCopy.setClientAddr(area);
                    }
                    // 3. 临时账户信息
                    AccountInfoBean accountInfoBean = accountInfoMapper.getAccountInfoById(pjMeter.getUserAccountTempId(), AccountInfoBean.ACCOUNTTEMP);
                    //  4. 临时用户信息
                    UserBean userBean = userInfoMapper.getUserInfoById(pjMeter.getUserInfoTempId(), UserBean.USERTEMP);
                    // 4.1 获取营业区域名称
                    String businessArea = userBean.getBusinessArea();
                    if (userBean != null && StringUtils.isNotEmpty(businessArea)) {
                        BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
                        BusinessAreaQueryBean businessAreaQueryBean = new BusinessAreaQueryBean();
                        businessAreaQueryBean.setId(businessArea);
                        BusinessAreaBean businessAreaBean = businessAreaMapper.getSingle(businessAreaQueryBean);
                        userBean.setBusinessAreaName(businessAreaBean.getAreaName());
                    }
                    // 4.2 setupMeterLocation->setupMeterLocationName
                    if (userBean != null && StringUtils.isNotEmpty(userBean.getSetupMeterLocation())) {
                        userBean.setSetupMeterLocationName(Tools.getDicNameByCodeAndValue(factory, "CTL", userBean.getSetupMeterLocation()));
                    }
                    // 4.3 clientContractAddr->clientContractAddrId;clientContractAddr转中文
                    if (!StringUtils.isEmpty(userBean.getClientContractAddr())) {
                        userBean.setClientContractAddrId(userBean.getClientContractAddr());
                        String area = pubAdminAreaMapper.selectFullNameById(userBean.getClientContractAddr());
                        userBean.setClientContractAddr(area);
                    }
                    pjMeter.setUserInfo(userBean);
                    // 5. 水表信息
                    String msInfoId = pjMeter.getMsInfoId();
                    // 20220223: 工程设计-施工计划期间，只有userInfo的相关信息;兼容msInfoId为空的情况
                    if (StringUtils.isNotEmpty(msInfoId)) {
                        MsInfoDto msInfoDto = msInfoMapper.getMsInfoById(pjMeter.getMsInfoId());
                        // 5.1 factoryName
                        MeterFactoryBean factoryBean = meterFactoryMapper.selectMeterFactoryById(msInfoDto.getMeterFactory());
                        msInfoDto.setMeterFactoryName(factoryBean == null ? msInfoDto.getMeterFactory() : factoryBean.getFactoryName());
                        // 5.2 meterBoreName
                        if (StringUtils.isNotBlank(msInfoDto.getMeterBore())) {
                            MeterBoreBean boreBean = meterBoreMapper.selectMeterBoreById(msInfoDto.getMeterBore());
                            msInfoDto.setMeterBoreName(boreBean == null ? msInfoDto.getMeterBore() : boreBean.getBoreName());
                        }
                        // 5.3 meterModelName
                        List<MeterModelVO> modelList = mapper.selectMeterModelList(new MeterModelQueryBean());
                        if (StringUtils.isNotBlank(msInfoDto.getMeterModel())) {
                            msInfoDto.setMeterModelName(modelList.stream().filter(y -> y.getId().equals(msInfoDto.getMeterModel())).findFirst().orElse(new MeterModelVO()).getModelName());
                            if (StringUtils.isBlank(msInfoDto.getMeterModelName()))
                                msInfoDto.setMeterModelName(msInfoDto.getMeterModel());
                        }
                        // 5.4 meterTypeName
                        if (StringUtils.isNotBlank(msInfoDto.getMeterType())) {
                            msInfoDto.setMeterTypeName(meterTypes.get(msInfoDto.getMeterType()));
                        }
                        pjMeter.setMsInfo(msInfoDto);
                    } else {
                        MsInfoDto msInfoDto = new MsInfoDto();
                        pjMeter.setMsInfo(msInfoDto);
                    }
                    // 6. 临时客户信息
                    pjMeter.setCtmInfo(ctmInfoBeanCopy);
                    // 7. 临时账户信息
                    pjMeter.setAccountInfo(accountInfoBean);
//                    // 8. 临时混合用水信息&&折扣信息
//                    // 8.1 构造请求
//                    UserMixtureQueryBean userMixtureQueryBean = new UserMixtureQueryBean();
//                    userMixtureQueryBean.setId(pjMeter.getId());
//                    userMixtureQueryBean.setType("2");
//                    userMixtureQueryBean.setUserNo(userBean.getUserNo());
//                    if (userBean.getUseWaterType().equals("6")) {
//                        UserMixtureMapper userMixtureMapper = factory.getMapper(UserMixtureMapper.class);
//                        // 8.2 查询混合用水数据
//                        List<UserMixtureBean> userMixtureBeanList = userMixtureMapper.listTemp(userMixtureQueryBean);
//                        // 8.3 返回混合用水数据
//                        UserMixtureVO userMixtureVO = new UserMixtureVO();
//                        userMixtureVO.setList(userMixtureBeanList);
//                        pjMeter.setUserMixtureData(userMixtureVO);
//                    }
//                    // 8.4 临时折扣信息
//                    UserDiscountMapper userDiscountMapper = factory.getMapper(UserDiscountMapper.class);
//                    List<UserDiscountBean> userDiscountBeanList = userDiscountMapper.listTemp(userMixtureQueryBean);
//                    // 8.5 返回折扣信息
//                    UserDiscountVO userDiscountVO = new UserDiscountVO();
//                    userDiscountVO.setList(userDiscountBeanList);
//                    pjMeter.setDiscountData(userDiscountVO);
                }
            }
            // 9. 返回结果
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户列表查询成功", List.class);
            messageBean.setData(pjMeterList);
            return messageBean;
        } catch (Exception e) {
            logger.error("立户列表查询失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "立户列表查询失败", null);
        }
    }
}
